#include "speedboard.h"
#include <QDebug>

/*
 * QT实现画扇形半透明
    startAngle = startAngle*16;           //startAngle算法跟其他的都是一致的
    spanAngle = spanAngle*16;
    QPainter painter(image);
    QBrush brush;
    painter.setPen(QPen(Qt::blue,5,Qt::DotLine));
    painter.setBrush(QBrush(Qt::CrossPattern));
    painter.setBrush(QBrush(QColor(245,36,27,128)));     //前三位表示色素，后面的一位表示透明度，越接近于255越不透明
    painter.drawPie(x1,y1,x2,y2, startAngle, spanAngle);
*/

SpeedBoard::SpeedBoard(QWidget *parent) : QWidget(parent)
{
  setFixedSize(231, 227);
}

void SpeedBoard::paintEvent(QPaintEvent *)
{
  QImage imageBackground(":/dashboard/background");

  QPainter painter(this);
  painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
  painter.drawImage(rect(), imageBackground);
  painter.save();

  //set new central
  painter.translate(CenterPoint_X, CenterPoint_Y);
  //旋转一个角度
  painter.rotate(neddleAngle - 180);
  painter.drawPixmap(0, 0, Neddle_W, Neddle_H, QPixmap(":/dashboard/speed_neddle"));
  painter.restore();

  painter.save();

  //set new central
  painter.translate(CenterPoint_X, CenterPoint_Y);
  //旋转一个角度
  painter.rotate(160);
  painter.drawPixmap(0, 0, Neddle_W, Neddle_H, QPixmap(":/dashboard/speed_neddle"));
  painter.restore();

  //绘制速度数字
  painter.translate(0, 0);
  painter.drawImage(86, 91, QImage(QString(":/dashboard/%1").arg(speedValue / 100)));
  painter.drawImage(105, 91, QImage(QString(":/dashboard/%1").arg(speedValue / 10 % 10)));
  painter.drawImage(124, 91, QImage(QString(":/dashboard/%1").arg(speedValue % 10)));

//  painter.setPen(QPen(Qt::blue,15,Qt::SolidLine));
//  painter.setBrush(QBrush(Qt::CrossPattern));
//  painter.setBrush(QBrush(QColor(245,36,27,30)));     //前三位表示色素，后面的一位表示透明度，越接近于255越不透明
//  painter.drawArc(0, 0, 200, 200, 0, 225*16);
}

void SpeedBoard::setSpeedValue(const qint16 &value)
{
  speedValue = value;
  neddleAngle = speedValue * Neddle_MaxAngle / Speed_Max;//(0-225)
  qDebug() << neddleAngle << value;

  update();
}
